package com.justgame.project.service.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import com.justgame.project.service.system.entity.SysDict;
import com.justgame.project.service.system.dto.SysDictDTO;
import com.justgame.project.service.system.wrapper.SysDictWrapper;
import com.justgame.project.service.system.controller.form.SysDictQueryForm;
import com.justgame.project.service.system.service.ISysDictService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * 系统字典控制器
 *
 * @@tableName sys_dict
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/dict")
@Api("系统字典控制器")
public class SysDictController {

    private final ISysDictService sysDictService;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 系统字典")
    public Result<?> query(@ApiParam("主键id") @NotNull(message = "必须提供需要查询的数据") Long id){
        return Result.ok(sysDictService.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 系统字典")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated SysDictDTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        Assert.notNull(dto.getParentId(),"必须提供父级id");
        Assert.notNull(dto.getCode(),"必须提供字典编号");
        Assert.notNull(dto.getKey(),"必须提供键");
        Assert.notNull(dto.getValue(),"必须提供值");
        Assert.notNull(dto.getRemark(),"必须提供备注");
        SysDict sysDict = sysDictService.requireGetById(dto.getId(),"相关数据不存在");
        sysDict.setParentId(dto.getParentId());
        sysDict.setCode(dto.getCode());
        sysDict.setKey(dto.getKey());
        sysDict.setValue(dto.getValue());
        sysDict.setRemark(dto.getRemark());
        return Result.status(sysDictService.updateById(sysDict));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 系统字典")
    public Result<?> save(@ApiParam("新增表单") @RequestBody @Validated SysDictDTO dto){
        Assert.notNull(dto.getParentId(),"必须提供父级id");
        Assert.notNull(dto.getCode(),"必须提供字典编号");
        Assert.notNull(dto.getKey(),"必须提供键");
        Assert.notNull(dto.getValue(),"必须提供值");
        Assert.notNull(dto.getRemark(),"必须提供备注");
        SysDict sysDict = new SysDict();
        sysDict.setParentId(dto.getParentId());
        sysDict.setCode(dto.getCode());
        sysDict.setKey(dto.getKey());
        sysDict.setValue(dto.getValue());
        sysDict.setRemark(dto.getRemark());
        return Result.status(sysDictService.save(sysDict));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 系统字典")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(sysDictService.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") SysDictQueryForm queryForm){
        IPage<SysDictWrapper> page = sysDictService.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") SysDictQueryForm queryForm){
        List<SysDictWrapper> list = sysDictService.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}